package evostar.controller;

import evostar.access.AccessToken;
import evostar.model.OrderInfo;
import evostar.model.User;
import evostar.rabbitmq.MQSender;
import evostar.rabbitmq.SeckillMessage;
import evostar.result.CodeMsg;
import evostar.result.Result;
import evostar.service.GoodsService;
import evostar.service.OrderService;
import evostar.service.RedisService;
import evostar.service.SeckillService;
import evostar.VO.GoodsVO;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping(value = "/seckill")
public class SeckillController implements InitializingBean {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private SeckillService seckillService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private MQSender sender;

    private final HashMap<Integer, Boolean> over = new HashMap<>();

    @RequestMapping(value = "/{path}")
    @ResponseBody
    @AccessToken
    public Result<Integer> seckill(User user, Model model, int goodsId, @PathVariable("path") String path) {
        model.addAttribute("user", user);
        if (!seckillService.verifyPath(path, user.getId(), goodsId)) {
            return Result.error(CodeMsg.ACCESS_ERROR);
        }
        if (over.get(goodsId)) {
            return Result.error(CodeMsg.STOCK_EMPTY);
        }
        //判断库存
        int stock = redisService.getValue(GoodsService.GoodStock + goodsId, Integer.class);
        if (stock <= 0) {
            over.put(goodsId, true);
            Result.error(CodeMsg.STOCK_EMPTY);
        }
        //库存预减1
        redisService.decrement(GoodsService.GoodStock + goodsId);
        //判断是否已经下过单
        OrderInfo order = orderService.getSeckillOrderByUserIdGoodsId(user.getId(), goodsId);
        if (order != null) {
            redisService.increment(GoodsService.GoodStock + goodsId);
            Result.error(CodeMsg.ORDER_AGAIN);
        }
        //执行入队列
        SeckillMessage message = new SeckillMessage();
        message.setUser(user);
        message.setGoodsId(goodsId);
        sender.seckill(message);
        return Result.success(0);

    }

    @Override
    public void afterPropertiesSet() throws Exception {
        List<GoodsVO> goodsList = goodsService.listGoodsVO();
        if (goodsList != null && goodsList.size() > 0) {
            for (GoodsVO goods : goodsList) {
                redisService.setValue(GoodsService.GoodStock + goods.getId(), goods.getSeckillStock(), 0);
                over.put(goods.getId(), false);
            }
        }
    }

    @RequestMapping(value = "/result")
    @ResponseBody
    public Result<Integer> result(User user, int goodsId) {
        int result = seckillService.getSeckillResult(user.getId(), goodsId);
        return Result.success(result);
    }

    @RequestMapping(value = "/path")
    @ResponseBody
    public Result<String> path(User user, int goodsId) {
        String path = seckillService.createPath(user.getId(), goodsId);
        return Result.success(path);
    }
}
